package com.optitronics.controllers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;

import com.optitronics.service.ResellerService;
import com.optitronics.ui.ResellerForm;
import com.optitronics.validators.ResellerValidator;

@Controller
@SessionAttributes("resetPasswordForm")
@RequestMapping("/resetPassword")
public class ResetPasswordFormController {

	@Autowired
	private ResellerValidator resellerValidator;

	@Autowired
	private ResellerService resellerService;

	@RequestMapping(method = RequestMethod.GET)
	public String setupForm(Model model) {

		model.addAttribute("resetPasswordForm", new ResellerForm());
		return "resellers/resetPasswordForm";

	}

	@RequestMapping(method = RequestMethod.POST)
	public String processSubmit(Model model, @ModelAttribute("resetPasswordForm") ResellerForm formObject,
			BindingResult result, SessionStatus status) {

		resellerValidator.validateResetPassowrd(formObject, result);
		if (result.hasErrors()) {
			return "resellers/resetPasswordForm";
		} else {
			resellerService.resetPassword(formObject);
			status.setComplete();
			model.addAttribute("message", "label.password.reseted");
			return "infoPage";
		}
	}

}
